package rs.etf.kdp.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JTextArea;

public class Connecter extends Thread {

	private Socket server;
	private ObjectInputStream input;
	private ObjectOutputStream output;
	private JTextArea status;
	String serverHost;
	int serverPort;
	boolean disconnect;
	ClientGUI gui;
	
	public Connecter(String serverHost, int serverPort, ClientGUI gui) {
		this.gui = gui;
		this.status = gui.txtStatus;
		this.serverHost = serverHost;
		this.serverPort = serverPort;
		disconnect = false;
	}
	
	public void run()
	{
		try {
			server = new Socket(serverHost, serverPort);
			status.append("\nSender: Created socket ");
		} catch (UnknownHostException e) {
			status.append("\nSender: Unknown host " + serverHost);
			return;
		} catch (IOException e) {
			status.append("\nSender: Unable to connect to " + serverHost + ":" + serverPort);
			return;
		}
		
		try {
			output = new ObjectOutputStream(server.getOutputStream()); //true if for auto flush
			output.flush();
			input = new ObjectInputStream(server.getInputStream());
		} catch (IOException e) {
			status.append("\nConnecter: Unable to get input/output stream ");
		}
		
		status.append("\nSender: Successfully connected to " + serverHost + ":" + serverPort);
		String message = "client";
		try {
			output.writeObject(message);
		} catch (IOException e1) {
			status.append("\nConnecter: Unable to send initial message ");
		}
		
		gui.socketToServer = server;
		gui.output = output;
		gui.input = input;
		gui.mntmRequestedJobs.setEnabled(true);
		gui.btnConnectToServer.setEnabled(true);
		
		if(ClientGUI.rec==null)
		{
			ClientGUI.rec= new Receiver(input, gui.txtStatus, gui.regBuff);
			ClientGUI.rec.start();
		}
		
		synchronized(this)
		{
			while(disconnect==false)
			{	
				try {
					wait();
				} catch (InterruptedException e) {
					status.append("\nInterrupted while was blocked in Connector");
				}
			}
		}
		
		try {
			if(server != null) server.close();
		} catch (IOException e) {
			status.append("\nSender: Unable to close socket ");
		}
		
		
		server = null;
		status.append("\nSender: disconnected " + serverHost + ":" + serverPort );
		return;
		
		
	}
	
	public synchronized void disconnect()
	{
		disconnect = true;
		notifyAll();
	}

	public Socket getServer() {
		return server;
	}

	public void setServer(Socket server) {
		this.server = server;
	}

	public ObjectInputStream getInput() {
		return input;
	}

	public void setInput(ObjectInputStream input) {
		this.input = input;
	}

	public ObjectOutputStream getOutput() {
		return output;
	}

	public void setOutput(ObjectOutputStream output) {
		this.output = output;
	}

	public JTextArea getStatus() {
		return status;
	}

	public void setStatus(JTextArea status) {
		this.status = status;
	}

	public String getServerHost() {
		return serverHost;
	}

	public void setServerHost(String serverHost) {
		this.serverHost = serverHost;
	}

	public int getServerPort() {
		return serverPort;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}
	
	
	
	
}
